/* -----------------
* brief
* 1. this is code gen by tools
 */

package service

import (
	"fmt"
	"github.com/mjiulee/lego"
	"github.com/mjiulee/lego/utils"
	"saas_sys_base/common"
	. "saas_sys_base/modules/orders/models"
)

type CartService struct {
	// add your model here
	tbCart Cart
}

func (t *CartService) ListCart(keyword string, page, psize int, orderField, order string, appwho *common.Who) ([]map[string]string, int64, error) {
	// 业务查询
	queryMap := map[string]interface{}{
		"table":  map[string]string{"table": "tb_cart", "alias": "t1"},
		"fields": []string{"t1.id","t1.product_id","t1.spec_id","t1.num","t2.product_name","t2.default_image","t3.spec_name","t3.sell_price","t3.stock","t2.if_sale"},
		"join": []map[string]string{
			map[string]string{"table": "tb_product as t2", "on": "t2.id = t1.product_id"},
			map[string]string{"table": "tb_product_spec as t3", "on": "t3.id = t1.spec_id"},
		},
		"where": []lego.QMapWhereField{
			{"t1.if_del", "=", 0, "AND", false},
			{"t1.user_id", "=", appwho.Uid, "AND", false},
			{"t1.tenant_id", "=", appwho.TenantId, "AND", false},
		},
		"search": map[string]string{
			"t2.product_name": keyword,
		},
		"orders": []string{"t1.id asc"},
		"page": map[string]int{
			"page":  page,
			"psize": psize,
		},
	}

	if orderField != "" && order != "" {
		orderStr := fmt.Sprintf("t1.%s %s", orderField, order)
		queryMap["orders"] = []string{orderStr, "t1.cdate asc"}
	}

	return lego.QueryListByMap(queryMap)
}

func (t *CartService) ListCartByWho(who *common.Who, keyword string, page, psize int, orderField, order string) ([]map[string]string, int64, error) {
	// 业务查询
	queryMap := map[string]interface{}{
		"table":  map[string]string{"table": "tb_cart", "alias": "t1"},
		"fields": []string{"t1.*"},
		"join":   []map[string]string{
			//map[string]string{"table": "tb_xxx as t2", "on": "t2.xxx = t1.xxx"},
		},
		"where": []lego.QMapWhereField{
			{"t1.if_del", "=", 0, "AND", false},
			//{"xx.tenant_id", "=", who.TenantId, "AND", false},
			//{"xx.mini_app_id", "=", who.MiniAppId, "AND", false},

		},
		"search": map[string]string{
			//"t1.id": req.Keyword,
		},
		"orders": []string{"t1.id asc"},
		"page": map[string]int{
			"page":  page,
			"psize": psize,
		},
	}

	if orderField != "" && order != "" {
		orderStr := fmt.Sprintf("t1.%s %s", orderField, order)
		queryMap["orders"] = []string{orderStr, "t1.cdate asc"}
	}
	/*{ //这段生成代码的目的是，当需要根据条件判断，来增加对应where条件时，使用这里的逻辑
		queryWhere := queryMap["where"].([]lego.QMapWhereField)
		if who.TenantId != 0 {
			queryWhere = append(queryWhere, lego.QMapWhereField{"t2.tenant_id", "=", who.TenantId, "AND", false})
		}

		if who.MiniAppId != 0 {
			queryWhere = append(queryWhere, lego.QMapWhereField{"t2.mini_app_id", "=", who.MiniAppId, "AND", false})
		}

	}*/

	return lego.QueryListByMap(queryMap)
}

/* 根据id获取记录
 * -----------------
 */
func (t *CartService) RecodeByWho(id int64,appwho *common.Who) *Cart {
	return t.tbCart.RecodeByWho(id,appwho)
}

/* 根据id获取记录
 * -----------------
 */
func (t *CartService) RecodeByProduct(spec_id int64,appwho *common.Who) *Cart {
	return t.tbCart.RecodeByProduct(spec_id,appwho)
}

/* 添加
 * -----------------
 */
func (t *CartService) SaveCart(item2add *Cart) bool {
	return t.tbCart.AddRecode(item2add)
}

/* 删除(硬删除)*/
func (t *CartService) DelCartById(id int64) bool {
	return t.tbCart.DelRecodeById(id)
}

/* 更新
 * -----------------*/
func (t *CartService) UpdateCart(rc *Cart) bool {
	return t.tbCart.UpdateRecode(rc)
}


func (t *CartService) SubmitOrder(appwho *common.Who,specIds []int64) bool {
var carts = t.tbCart.GetCartsByWho(appwho)
	for _, v := range specIds{
		cart, _ := utils.SliceFindFirstWhere(carts, func(elem interface{}) bool {
			item := elem.(*Cart)
			if v == item.SpecId {
				return true
			}
			return false
		})

		if cart != nil{
			cartItem := cart.(*Cart)
			t.tbCart.DelRecodeById(cartItem.Id)
		}

	}
	return true
}